package com.spzx.manager.controller;

import com.spzx.manager.service.SysMenuService;
import com.spzx.manager.service.SysUserService;
import com.spzx.manager.service.ValidateCodeService;
import com.spzx.model.dto.system.LoginDto;
import com.spzx.model.entity.system.SysUser;
import com.spzx.model.vo.common.Result;
import com.spzx.model.vo.common.ResultCodeEnum;
import com.spzx.model.vo.system.LoginVo;
import com.spzx.model.vo.system.SysMenuVo;
import com.spzx.model.vo.system.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Project: spzx-parent
 * Package: com.spzx.manager.controller
 * Version: 1.0
 * <p>
 * Created by  wangjiaxin  on 2023/11/30 20:30
 */
@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 用户登陆
     *
     * @param loginDto
     * @return
     */
    @Operation(summary = "登录接口")
    @PostMapping(value = "/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 生成图片验证码
     *
     * @return
     */
    @RequestMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取当前用户信息
     *
     * @param token 要从请求头中获取token信息
     * @return
     * @RequestHeader 通过注解获取前端的请求信息
     */
    @RequestMapping(value = "/getUserInfo")
    public Result<SysUser> getUserInfo(@RequestHeader(name = "token") String token) {
        SysUser sysUser = sysUserService.getUserInfo(token);
        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
        //在登陆拦截的时候会进行验证，将用户信息放入到了ThreadLocal线程对象中，这里可以直接获取
//        return Result.build(AuthContextUtil.get(), ResultCodeEnum.SUCCESS);
    }


    /**
     * 退出登陆
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "token") String token) {
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 动态菜单显示
     * 查询可以操作的菜单
     *
     * @return
     */
    @GetMapping("/menus")
    public Result menus() {
        List<SysMenuVo> sysMenuVoList = sysMenuService.findUserMenuList();
        return Result.build(sysMenuVoList, ResultCodeEnum.SUCCESS);
    }
}
